/* bsp_config.h */
#ifndef __BSP_CONFIG_H__
#define __BSP_CONFIG_H__

#include "hal_common.h"
#include "hal_clock.h"
#include "hal_lpuart.h"

#include <stdio.h>

#define CLOCK_SETUP 3

#ifndef CLOCK_SETUP
#define CLOCK_SETUP 0
#endif /* CLOCK_SETUP */

#if CLOCK_SETUP==0

#elif CLOCK_SETUP==1

#define BSP_CLK_OSCCLK_HZ    8000000UL
#define BSP_CLK_CORECLK_HZ   150000000UL
#define BSP_CLK_BUSCLK_HZ    75000000UL

#define BSP_CLK_SPLLDIV2_HZ  75000000UL
#define BSP_CLK_SOSCDIV2_HZ  /* External OSC, not used. */
#define BSP_CLK_SIRCDIV2_HZ  /* Disabled. */
#define BSP_CLK_FIRCDIV2_HZ  48000000UL

#elif CLOCK_SETUP==2

#define BSP_CLK_SPLLDIV2_HZ  320000000UL
#define BSP_CLK_SOSCDIV2_HZ  /* External OSC, not used. */
#define BSP_CLK_SIRCDIV2_HZ  /* Disabled. */
#define BSP_CLK_FIRCDIV2_HZ  48000000UL

#define BSP_CLK_OSCCLK_HZ    8000000UL
#define BSP_CLK_CORECLK_HZ   160000000UL
#define BSP_CLK_BUSCLK_HZ    80000000UL

#elif CLOCK_SETUP==3

#define BSP_CLK_OSCCLK_HZ    12000000UL  /* 12MHz. */
#define BSP_CLK_CORECLK_HZ   168000000UL /* 168MHz. System Clock.*/
#define BSP_CLK_BUSCLK_HZ    84000000UL  /* 84MHz. */
#define BSP_CLK_FLASHCLK_HZ  21000000UL  /* 21MHz. Slow Divider. */

#define BSP_CLK_SPLLDIV1_HZ  42000000UL  /* 42MHz. */
#define BSP_CLK_SPLLDIV2_HZ  84000000UL  /* 84MHz. */

#endif /* CLOCK_SETUP */

/*
* LPUART.
*/
#define BSP_LPUART_DEBUG_PORT LPUART0
#define BSP_LPUART_BLE_PORT   LPUART1

/*
* GPIO.
*/
#define BSP_GPIO_WS2812B_RIGHT_PORT GPIOC
#define BSP_GPIO_WS2812B_RIGHT_PIN  3U
#define BSP_GPIO_WS2812B_LEFT_PORT  GPIOC
#define BSP_GPIO_WS2812B_LEFT_PIN   2U

#define BSP_GPIO_BLE_WAKEUP_PORT    GPIOA
#define BSP_GPIO_BLE_WAKEUP_PIN     13U
#define BSP_GPIO_BLE_DISCON_PORT    GPIOA
#define BSP_GPIO_BLE_DISCON_PIN     12U

/*
* ADC.
*/
#define BSP_ADC_BATTERY_SENSOR_PORT          ADC0
#define BSP_ADC_BATTERY_SENSOR_CAHNNEL_GROUP 0U
#define BSP_ADC_BATTERY_SENSOR_CAHNNEL_INDEX 8U

/*
* Motor.
*/
/* 控制是否切断电机驱动器内部电源 */
#define BSP_GPIO_MOTOR_STANDBY_PORT         GPIOD
#define BSP_GPIO_MOTOR_STANDBY_PIN          5U
#define BSP_FTM_MOTOR_PWM_PORT              FTM0

/* MOTOR A. */
/* 控制方向 */
#define BSP_GPIO_MOTOR_A_IN1_PORT           GPIOE
#define BSP_GPIO_MOTOR_A_IN1_PIN            8U
#define BSP_GPIO_MOTOR_A_IN2_PORT           GPIOE
#define BSP_GPIO_MOTOR_A_IN2_PIN            9U
/* 控制转速 */
#define BSP_FTM_MOTOR_A_PWM_CHANNEL_NUM     1U
/* 编码器接口 */
#define BSP_FTM_MOTOR_A_QUAD_DECODER_PORT   FTM2

/* MOTOR B. */
/* 控制方向 */
#define BSP_GPIO_MOTOR_B_IN1_PORT           GPIOB
#define BSP_GPIO_MOTOR_B_IN1_PIN            4U
#define BSP_GPIO_MOTOR_B_IN2_PORT           GPIOB
#define BSP_GPIO_MOTOR_B_IN2_PIN            5U
/* 控制转速 */
#define BSP_FTM_MOTOR_B_PWM_CHANNEL_NUM     0U
/* 编码器接口 */
#define BSP_FTM_MOTOR_B_QUAD_DECODER_PORT   FTM1

/* Motor ID Mask.*/
#define MOTOR_ID_A   (1U << 0U)
#define MOTOR_ID_B   (1U << 1U)

/*
* RC.
*/
/* 解调RC接收机PPM信号 */
#define BSP_GPIO_RC_SPEED_CAPTURE_CHANNEL_NUM    6U
#define BSP_GPIO_RC_TURN_CAPTURE_CHANNEL_NUM     7U
#define BSP_FTM_RC_CAPTURE_PORT                  FTM3

/* 在扩展接口上的GPIO模拟SPI适配的SD卡 */

/* 使用GPIO软件模拟SPI */
/* 使用扩展接口的引脚
 * PTE2  SPI_SIN
 * PTA2  SPI_SOUT
 * PTD6  SPI_CLK
 * PTE11 SPI_CS
 */

//#define BSP_GPIO_SDCARD_SPI_SOUT_PORT  GPIOA
//#define BSP_GPIO_SDCARD_SPI_SOUT_PIN   2u

//#define BSP_GPIO_SDCARD_SPI_SIN_PORT   GPIOE
//#define BSP_GPIO_SDCARD_SPI_SIN_PIN    2u

//#define BSP_GPIO_SDCARD_SPI_CLK_PORT   GPIOD
//#define BSP_GPIO_SDCARD_SPI_CLK_PIN    6u

#define BSP_GPIO_SDCARD_SPI_CS_PORT    GPIOB
#define BSP_GPIO_SDCARD_SPI_CS_PIN     0u

/*
* API.
*/
void BSP_InitSystem(void);
void BSP_InitLpuartForDebug(void);
void BSP_InitStdioLPUART(uint32_t baudrate);
void BSP_InitWs2812bGpio(void);

/* 配置蓝牙模块 */
void BSP_InitLpuartForBLE(void);
void BSP_InitGpioForBLE(void);

void BSP_InitAdcBatterySensor(void);
void BSP_InitMotorTurn(void);
void BSP_InitMotorQuadDecoder(void);

void BSP_InitRcDemodulator(void);

/* 使用扩展引脚 */
void BSP_InitExt_SDCard(void);

#endif /* __BSP_CONFIG_H__ */
